use alex_album::{
    bootstrap::{self, config, AppState},
    router,
};

#[tokio::main]
async fn main() {
    if std::env::var_os("RUST_LOG").is_none() {
        std::env::set_var("RUST_LOG", "alex_album=debug,sea_orm=debug");
    }
    let _ = dotenv::dotenv().ok();

    tracing_subscriber::fmt::init();

    let cfg = config::Configuration::from_env().unwrap();

    println!("{}", cfg.database.url);

    let db_pool = bootstrap::gen_db_pool(cfg.database.url).await.unwrap();
    let state = AppState { db_pool };

    let address = format!("{}:{}", cfg.web.host, cfg.web.port);
    tracing::info!("http port on => {}", address);

    let listener = tokio::net::TcpListener::bind(address).await.unwrap();

    let routes = router::create_routes(state.clone())
        .await
        // .layer(session_layer)
        .with_state(state.clone());

    axum::serve(listener, routes).await.unwrap()
}
